﻿using System;
using System.Linq;
using UnityEngine;
using Zero;

[DefaultExecutionOrder(-10000)]
public class ElementService : MonoBehaviour, IElementService
{
    [Serializable]
    public class ElementalColorItem
    {
        [SerializeField] private ElementType _elementType;
        [SerializeField] private Color _color;

        public ElementType ElementType => _elementType;
        public Color Color => _color;
    }

    [SerializeField] private ElementalColorItem[] _elementalColorItems;

    [Serializable]
    public class ElementalRestraintItem
    {
        [SerializeField] private ElementType _elementType;
        [SerializeField] private ElementType[] _restraintElementTypes;
        [SerializeField] private ElementType[] _beRestraintElementTypes;

        public ElementType ElementType => _elementType;
        public ElementType[] RestraintElementTypes => _restraintElementTypes;
        public ElementType[] BeRestraintElementTypes => _beRestraintElementTypes;
    }

    [SerializeField] private ElementalRestraintItem[] _elementalRestraintItems;

    private void OnEnable()
    {
        ServiceLocator.Register<IElementService>(this);
    }

    private void OnDisable()
    {
        ServiceLocator.Unregister<IElementService>();
    }

    public Color GetColor(ElementType elementType)
    {
        foreach (var elementalColorItem in _elementalColorItems)
        {
            if (elementType == elementalColorItem.ElementType)
            {
                return elementalColorItem.Color;
            }
        }

        return Color.white;
    }

    public ElementalRestraintType CheckRestraint(ElementType attackerElement, ElementType defenderElement)
    {
        foreach (var elementalRestraintItem in _elementalRestraintItems)
        {
            if (attackerElement == elementalRestraintItem.ElementType)
            {
                if (elementalRestraintItem.RestraintElementTypes.Contains(defenderElement))
                {
                    return ElementalRestraintType.Restraint;
                }

                if (elementalRestraintItem.BeRestraintElementTypes.Contains(defenderElement))
                {
                    return ElementalRestraintType.BeRestraint;
                }
            }
        }

        return ElementalRestraintType.None;
    }

    public ElementType[] GetRestraintElements(ElementType element)
    {
        foreach (var elementalRestraintItem in _elementalRestraintItems)
        {
            if (element == elementalRestraintItem.ElementType)
            {
                return elementalRestraintItem.RestraintElementTypes;
            }
        }

        throw new ArgumentException("元素不存在", nameof(element));
    }

    public ElementType[] GetBeRestraintElements(ElementType element)
    {
        foreach (var elementalRestraintItem in _elementalRestraintItems)
        {
            if (element == elementalRestraintItem.ElementType)
            {
                return elementalRestraintItem.BeRestraintElementTypes;
            }
        }

        throw new ArgumentException("元素不存在", nameof(element));
    }

    // /// <summary>
    // /// 获得克制伤害比例
    // /// </summary>
    // /// <param name="self"></param>
    // /// <param name="element"></param>
    // /// <returns></returns>
    // public static float GetRestraintDamageRatio(this ElementType self, ElementType element) =>
    //     self.IsRestraint(element)
    //         ? 1.25f // 克制
    //         : self.IsBeRestraint(element)
    //             ? 0.8f // 被克制
    //             : 1f;
}